This is one of the noobiest questions you will ever find, but in my Android app, I need to have a few different "screens"; one where you log in, and a view that represents a question (there will be multiple questions, so I need to make a view for each question and have it change to the next view when a next button is clicked. Let me explain with a flow chart:
User Opens App -> Login Screen -> User Logs In -> View changes to auto generated view for question 1 -> Next button clicked -> question 2 -> does all questions -> Final view that says you are done.
I have created two xml layout files, one called main and one called quiz (which represents a question).
I will elaborate on anything needed.
UPDATE
Here are my two activity classes:
package me.nrubin29.quiz.student;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button done = (Button) findViewById(R.id.done);
final EditText serverAddress = (EditText) findViewById(R.id.serverAddress);
final EditText secretNumber = (EditText) findViewById(R.id.secretNumber);
final EditText name = (EditText) findViewById(R.id.name);
done.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String addr = serverAddress.getText().toString();
String num = secretNumber.getText().toString();
String n = name.getText().toString();
if (addr.equals("") || num.equals("") || n.equals("")) {
Toast.makeText(getApplicationContext(), "You didn't fill in all the fields.", Toast.LENGTH_LONG).show();
return;
}
new Connection().initConnection(Main.this, addr, num, n);
}
});
}
}
package me.nrubin29.quiz.student;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
public class Quiz extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton);
final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton1);
final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioButton2);
final RadioButton radioButton4 = (RadioButton) findViewById(R.id.radioButton3);
radioButton1.setText("Testing!");
}
}
And here are my two XMLs:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Please enter the server address and secret number your teacher gives you."
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/serverAddress" android:layout_gravity="center"
android:phoneNumber="true"
android:hint="Server Address"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/secretNumber" android:layout_gravity="center"
android:phoneNumber="true"
android:hint="Secret Number"/>
<Space
android:layout_width="fill_parent"
android:layout_height="20px"
android:id="#+id/space1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please enter your name."
android:id="#+id/textView2" android:layout_gravity="left|center_vertical"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/name" android:layout_gravity="left|center_vertical"
android:hint="Name"/>
<Space
android:layout_width="fill_parent"
android:layout_height="20px"
android:id="#+id/space"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Done"
android:id="#+id/done" android:layout_gravity="center"/>
</LinearLayout>
<?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"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Question"
android:id="#+id/textView"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="#+id/radioButton"
android:layout_gravity="center"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="#+id/radioButton1"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="#+id/radioButton2"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="#+id/radioButton3"/>
</RadioGroup>
</LinearLayout>
UPDATE! And here's my newer Connection class:
package me.nrubin29.quiz.student;
import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
import me.nrubin29.quiz.student.receivedQuiz.Quiz;
import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class Connection {
private Connection() { }
private static Connection instance = new Connection();
public static Connection getInstance() {
return instance;
}
private Quiz quiz;
private Socket socket;
private Thread reader;
private ObjectInputStream inputStream;
private ObjectOutputStream outputStream;
public void initConnection(final Activity activity, final String ip, final String port, final String name) {
new Thread(new Runnable() {
public void run() {
try {
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity.getApplicationContext(), "Starting connection to " + ip + ":" + Integer.parseInt(port), Toast.LENGTH_SHORT).show();
}
});
socket = new Socket(ip, Integer.parseInt(port));
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity.getApplicationContext(), "Connected!", Toast.LENGTH_SHORT).show();
}
});
outputStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
outputStream.writeObject(name);
quiz = new Quiz((String) inputStream.readObject());
Intent i = new Intent(activity, me.nrubin29.quiz.student.Quiz.class);
activity.startActivity(i);
reader = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Object in = inputStream.readObject();
}
catch (final EOFException e) { activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(activity.getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } }); }
catch (Exception e) { e.printStackTrace(); }
}
}
});
reader.start();
}
catch (Exception e) { e.printStackTrace(); }
}
}).start();
}
public Quiz getQuiz() {
return this.quiz;
}
}
As far as the Activity for Quiz, I guess I should make a constructor that takes the question and answers and sets the text of the radio buttons? Also, how exactly (code example would rock) would I create go about answering the question with the code above?
For the login Activity just use whatever you layout you want with the Views needed (Spinner, TextView, Button, etc...).
If the question screens will all be the same then you can just use Fragments coupled with ViewPager if you want. This should work nicely for your needs. This will allow you to supply new data for each question but easily keep the same layout.
VeiwPager Docs
ViewPager example I haven't looked at it much but his tutorials are usually pretty good.
If ViewPager is more than you need/want right now, you can simply have one View for the question such as a TextView and change the text when the Button is clicked.
Edit
Change your Intent to use that Context
Intent i = new Intent(activity, Quiz.class);
activity.startActivity(i);
You can implement this by having two different Activities and each having different content views (XML Files).
The default Launcher activity will be your Login Screen with the login layout file.
The second activity should be having a view to handle questions. This can be done using your apt view groups like RelativeLAyout, LinearLayout, FrameLayout and so on. There are numberous examples on each in the internet. Just do a search for tutorials on each.
Final view (you are done) can be implemented in the same activity with a different view or just with a AlertDialog.
Related
I am making an Android app for storing notes. I have made my editText behave like textView before button click. Clicking on edit button makes the editText take value. Now problem arises when i save that value and when it is displayed. EditText doesnt come in multi-line as I want but in a single line.
I declared the inputType property of editText as multi-line in xml file but in the code upon initialising the editText i set its inputType property as null in an attempt to make it both null and multi-line but alas! It doesn't happen.
Below is my xml and java code:
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/AppTheme.AppBarOverlay"
android:background="#color/colorToolBar"
android:id="#+id/idToolbarNoteDetails"
android:padding="10dp"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/idFloatingActionButtonEdit"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
app:srcCompat="#android:drawable/ic_menu_edit"
android:layout_margin="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Colors: "
android:textStyle="italic"
android:visibility="invisible"
android:id="#+id/idTextViewColors"
android:layout_below="#+id/idToolbarNoteDetails"
android:layout_margin="10dp"
android:textSize="15dp"/>
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/idColorRed"
android:visibility="invisible"
android:layout_margin="10dp"
android:layout_below="#+id/idToolbarNoteDetails"
android:layout_toRightOf="#+id/idTextViewColors"
android:background="#drawable/textview_circle_red" />
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:visibility="invisible"
android:id="#+id/idColorBlue"
android:layout_margin="10dp"
android:layout_below="#id/idToolbarNoteDetails"
android:layout_toRightOf="#+id/idColorRed"
android:background="#drawable/textview_circle_blue"/>
<TextView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/idColorGreen"
android:visibility="invisible"
android:layout_margin="10dp"
android:layout_below="#id/idToolbarNoteDetails"
android:layout_toRightOf="#+id/idColorBlue"
android:background="#drawable/textview_circle_green"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/idTextViewColors"
android:layout_margin="10dp"
android:inputType="textShortMessage"
android:background="#android:color/transparent"
android:textSize="40sp"
android:hint="Title"
android:textStyle="bold"
android:id="#+id/idEditTextTitle"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/idEditTextTitle"
android:hint="Content"
android:inputType="textMultiLine"
android:lines="5"
android:gravity="top|left"
android:layout_margin="10dp"
android:textSize="20sp"
android:background="#android:color/transparent"
android:id="#+id/idEditTextContent"/>
<EditText
android:layout_width="250dp"
android:id="#+id/idEditTextTag"
android:layout_height="35dp"
android:hint="#TAG"
android:background="#android:color/transparent"
android:layout_alignParentBottom="true"
android:textSize="15dp"
android:textStyle="italic"
android:layout_margin="10dp"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/idFloatingActionButtonSave"
android:layout_above="#+id/idFloatingActionButtonEdit"
android:layout_margin="20dp"
android:visibility="invisible"
app:srcCompat="#android:drawable/ic_menu_save"
android:layout_alignParentRight="true"/>
JAVA
package mynotes.pawanjotsingh.com.mynotes.activities;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import mynotes.pawanjotsingh.com.mynotes.R;
import mynotes.pawanjotsingh.com.mynotes.adapters.RecyclerAdapter;
import mynotes.pawanjotsingh.com.mynotes.dbhelper.DataBaseHelper;
import mynotes.pawanjotsingh.com.mynotes.models.NoteModel;
public class NoteDetailsActivity extends AppCompatActivity {
enum Colors{RED, BLUE, GREEN, WHITE}
DataBaseHelper dataBaseHelper;
FloatingActionButton floatingActionButtonEdit,floatingActionButtonSave;
EditText editTextTitle,editTextContent,editTextTag;
String stringTitle,stringContent,stringTag;
TextView textViewColors,textViewRed,textViewBlue,textViewGreen;
Toolbar toolbar;
int color,colorRed,colorBlue,colorGreen;
String id="";
Colors currentBackgroundColour = Colors.WHITE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_details);
editTextTitle=(EditText) findViewById(R.id.idEditTextTitle);
editTextTitle.setInputType(InputType.TYPE_NULL);
editTextContent=(EditText) findViewById(R.id.idEditTextContent);
editTextContent.setInputType(InputType.TYPE_NULL);
editTextTag=(EditText) findViewById(R.id.idEditTextTag);
editTextTag.setInputType(InputType.TYPE_NULL);
toolbar=(Toolbar) findViewById(R.id.idToolbarNoteDetails);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Edit");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
dataBaseHelper=new DataBaseHelper(this);
textViewColors=(TextView) findViewById(R.id.idTextViewColors);
textViewRed=(TextView) findViewById(R.id.idColorRed);
textViewBlue=(TextView) findViewById(R.id.idColorBlue);
textViewGreen=(TextView) findViewById(R.id.idColorGreen);
colorRed=Color.parseColor("#FE7676");
colorBlue=Color.parseColor("#76FEEC");
colorGreen=Color.parseColor("#99FE76");
stringTitle=getIntent().getStringExtra("text_title");
stringContent=getIntent().getStringExtra("text_content");
stringTag=getIntent().getStringExtra("text_tag");
id = getIntent().getStringExtra("id");
color=getIntent().getIntExtra("color", Color.WHITE);
getWindow().getDecorView().setBackgroundColor(color);
editTextTitle.setText(stringTitle);
editTextContent.setText(stringContent);
editTextTag.setText(stringTag);
floatingActionButtonSave=(FloatingActionButton) findViewById(R.id.idFloatingActionButtonSave);
floatingActionButtonSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
save();
}
});
floatingActionButtonEdit=(FloatingActionButton) findViewById(R.id.idFloatingActionButtonEdit);
floatingActionButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
floatingActionButtonSave.setVisibility(View.VISIBLE);
Animation animationLeft=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_left);
floatingActionButtonSave.startAnimation(animationLeft);
textViewColors.setVisibility(View.VISIBLE);
Animation animationDown= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_down);
textViewColors.startAnimation(animationDown);
textViewRed.setVisibility(View.VISIBLE);
textViewRed.startAnimation(animationDown);
textViewBlue.setVisibility(View.VISIBLE);
textViewBlue.startAnimation(animationDown);
textViewGreen.setVisibility(View.VISIBLE);
textViewGreen.startAnimation(animationDown);
editNote();
}
});
textViewRed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentBackgroundColour=Colors.RED;
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FE7676"));
}
});
textViewBlue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentBackgroundColour=Colors.BLUE;
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#76FEEC"));
}
});
textViewGreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentBackgroundColour = Colors.GREEN;
getWindow().getDecorView().setBackgroundColor(Color.parseColor("#99FE76"));
}
});
}
#Override
public void onBackPressed() {
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
public void editNote(){
editTextTitle.setInputType(InputType.TYPE_CLASS_TEXT);
editTextTitle.setFocusable(true);
editTextTitle.requestFocus();
editTextContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editTextTag.setInputType(InputType.TYPE_CLASS_TEXT);
}
public void save() {
int colour=Color.WHITE;
switch (currentBackgroundColour){
case RED:
colour = colorRed;
break;
case BLUE:
colour = colorBlue;
break;
case GREEN:
colour = colorGreen;
break;
}
final NoteModel noteModel = new NoteModel();
noteModel.setTitle(editTextTitle.getText().toString());
noteModel.setContent(editTextContent.getText().toString());
noteModel.setTag(editTextTag.getText().toString());
noteModel.setId(id);
noteModel.setDate(getDateTime());
noteModel.setColor(colour);
boolean isModified=dataBaseHelper.editNote(noteModel);
if(isModified==true){
Toast.makeText(this, "Modifications saved", Toast.LENGTH_SHORT).show();
finish();
}
else Toast.makeText(this, "Unable to save modifications", Toast.LENGTH_SHORT).show();
}
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
Links to screenshots
This image is when I click edit button. This works just fine, text goes to another line.
This image is when i click the save button. The text goes back to single line instead of multi-line.
Okay so, I knew that the problem arises when I click on the edit button. Clicking the button made the editText single lined automatically even though I had mentioned the InputType as Multi-line in XML very clearly.
What I did to make the editText multi-line after the button click is that I wrote editTextContent.setSingleLine(false); in my java file inside the onClick button code.
And that is it. :)
I am trying to use an intent to start two different classes in which I change either the text size or text colour of an entered message. Starting these classes is triggered by two different button push events in android studio. However, only the TextSize class is being started no matter what button I push and I cannot see the reason why. Below I have the problem activities class code and xml code
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static final String EXTRAA_MESSAGE ="message2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeText(View v){
EditText messageView = (EditText)findViewById(R.id.message);
String messageText = messageView.getText().toString();
Intent result = new Intent(this, TextSize.class);
result.putExtra("message",messageText);
startActivity(result);
}
public void changeColour(View v){
EditText messageView = (EditText)findViewById(R.id.message);
String messageText = messageView.getText().toString();
Intent result = new Intent(this, ColourChange.class);
result.putExtra("message",messageText);
startActivity(result);
}
My XML File
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp" />
<EditText
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:ems="10"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true" />
<Button
android:text="Change Colour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Colour"
android:onClick="changeColour"
android:layout_centerVertical="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:onClick="changeText"
android:text="Change Text Size"
android:layout_alignBaseline="#+id/Colour"
android:layout_alignBottom="#+id/Colour"
android:layout_alignParentEnd="true"
android:layout_marginEnd="33dp"
android:id="#+id/TextSize" />
In Android just implements the OnClickListener for you when you define the android:onClick="someMethod" attribute.
you can implemented just different way.
Button btnColour = (Button) findViewById(R.id.Colour);
btnColour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// call your method here
changeColour(v);
}
});
// your method code
public void changeColour(View v) {
// your method code
}
As You doing
check your xml file that have you define context with parent layout or not
tools:context="com.example.YourActivity"
// com.example.YourActivity should be your activity with package name
I use this code
TextView.animate().setDuration(0).scaleX(scale).scaleY(scale).start();
When scale value increase to some point, it make TextView disappear. I dont know why, and how to prevent it?
[Edit] Attach my test code
[test_text_scale.xml] It is Layout xml file for TestTextScale.java
<?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"
android:clipChildren="false"
android:orientation="vertical">
<TextView
android:id="#+id/tv_test_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Test" />
<Button
android:id="#+id/bt_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="#+id/bt_down"
android:layout_toRightOf="#+id/bt_down"
android:text="Up" />
<Button
android:id="#+id/bt_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Down" />
</RelativeLayout>
[TestTextScale.java]
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TestTextScale extends AppCompatActivity {
// View
private TextView mTvTestZoom;
private Button mBtUp, mBtDown;
// Model
private int mCurrentScale = 1;
// OnClick listener
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_up : {
upScale();
setText();
} break;
case R.id.bt_down : {
downScale();
setText();
} break;
}
}
};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_text_scale);
initView();
initEvent();
}
private void initView() {
mTvTestZoom = (TextView) findViewById(R.id.tv_test_scale);
mBtUp = (Button) findViewById(R.id.bt_up);
mBtDown = (Button) findViewById(R.id.bt_down);
}
private void initEvent() {
mBtDown.setOnClickListener(mOnClickListener);
mBtUp.setOnClickListener(mOnClickListener);
}
private void upScale() {
mCurrentScale += 1;
mTvTestZoom.animate().setDuration(0).scaleX(mCurrentScale).scaleY(mCurrentScale).start();
}
private void downScale() {
mCurrentScale -= 1;
mTvTestZoom.animate().setDuration(0).scaleX(mCurrentScale).scaleY(mCurrentScale).start();
}
private void setText() {
mTvTestZoom.setText(mCurrentScale + "");
}
}
This may happen because the textview exceeds the layout bounds of its parent. To overcome this, simple put android:clipChildren="false" in the xml description of the immediate parent of the textview. You may also need to do it for their parents too.
EDIT:
Upon testing the code myself, I found that the textview did disappear with following log message:
E/OpenGLRenderer: Font size too large to fit in cache. width, height = 635, 1028
This is due to a possible issue in the Android Hardware acceleration modules. One of the ways to avoid this is:
mTvTestZoom.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
This works well and the textView does not disappear after a certain size but, since it disables hardware acceleration for the specific textview, it may lose antialiasing.
I have created a simple browser application with a edittext , go button and history button.
Please suggest what coding do i need to implement for history button such that when i press it my browsing history gets displayed on the hi.xml layout. Please suggest what coding changes do i need to make in my xml file as well as java file .
MainActivity.java:
package com.example.history;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
EditText t;
Button g,h;
WebView w;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g=(Button) findViewById(R.id.button1);
h=(Button) findViewById(R.id.button2);
t=(EditText) findViewById(R.id.editText1);
w=(WebView) findViewById(R.id.webView1);
h.setOnClickListener(this);
g.setOnClickListener(this);
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setLoadWithOverviewMode(true);
w.getSettings().setUseWideViewPort(true);
w.setWebViewClient(new Callback());
}
#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;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.button1:
String c;
c=t.getText().toString();
String theWebsite=("http://").concat(c);
t.setText(theWebsite);
w.loadUrl(theWebsite);
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(w.getWindowToken(),0);
break;
case R.id.button2:
Intent a=new Intent(MainActivity.this,h.class);
startActivity(a);
break;
}
}
}
class Callback extends WebViewClient{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
activity_main.xml:
<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"
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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginLeft="15dp"
android:layout_marginTop="16dp"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginTop="34dp"
android:layout_toRightOf="#+id/textView1"
android:text="Go" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_centerVertical="true"
android:text="History" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
h.java:
package com.example.history;
import android.app.Activity;
import android.os.Bundle;
public class h extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.hi);
}
}
hi.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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
When you open a URL using the WebView it gets stored in the phone's web history.
You can access it by using the below code.Later add it to the listview you use in hi.xml
try{
Cursor mCur = null;
try{
String sortOrder = Browser.BookmarkColumns.DATE + " ASC";
mCur= context.getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, sortOrder);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false) {
String title = mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
String url = mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
long date = mCur.getLong(Browser.HISTORY_PROJECTION_DATE_INDEX);
mCur.moveToNext();
}
}else{
mCur.close();
}
}catch(Exception e){
}finally{
mCur.close();
}
}catch(Exception e){
}
Also you need to give the below permission in your manifest file. Hope this helps.
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
You need to implement the onPageFinished Method on the WebView Client Class
and you need to save your loaded url to the localdatabase or if you want that history for perticular session of the app then you can also use the other structure like arraylist...
class Callback extends WebViewClient{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
/// Save the url to the localdatabase or to any structure if you want temporary
}
}
and while user press on the history button you need to fetch the data from localdatabase to cursor
and use cursoradapter to display the list of the history url
If you have any further doubt then feel free to ask me...
All my other onClick methods work except the ones in which I have to inflate the layout to get the button! What am I doing wrong! Here is some code:
package com.games.think;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TableLayout;
public class Think extends Activity{
//What question are we on?
int question = 1;
//What level are we on?
String lvl ="1";
//Radio buttons we need to access them global
RadioButton lvl1;
RadioButton lvl2;
RadioButton lvl3;
RadioButton lvl4;
RadioButton lvl5;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//play
Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
playOnClick(v);
}
});
//level
Button level = (Button)findViewById(R.id.level);
level.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
levelOnClick(v);
}
});
//setLevel
LayoutInflater inflater = LayoutInflater.from(this);
TableLayout tbl = new TableLayout(this);
View playv = inflater.inflate(R.layout.level, null);
Button updateLevel = (Button) playv.findViewById(R.id.updateLevel);
tbl.addView(updateLevel);
updateLevel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateLevelOnClick(v);
}
});
View levelv = inflater.inflate(R.layout.play, null);
Button gotomenu = (Button) levelv.findViewById(R.id.tomenu);
gotomenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toMenuOnClick(v);
}
});
//Radio Buttons
lvl1 = (RadioButton) levelv.findViewById(R.id.lvl1);
lvl2 = (RadioButton) levelv.findViewById(R.id.lvl2);
lvl3 = (RadioButton) levelv.findViewById(R.id.lvl3);
lvl4 = (RadioButton) levelv.findViewById(R.id.lvl4);
lvl5 = (RadioButton) levelv.findViewById(R.id.lvl5);
//tomenu
lvl = getLevel();
if(lvl.equals("-1")) {
lvl=getLevel();
}
}
protected void toMenuOnClick(View v) {
setContentView(R.layout.main);
}
protected void updateLevelOnClick(View v) {
setContentView(R.layout.main);
}
protected void levelOnClick(View v) {
setContentView(R.layout.level);
if(lvl.equals("1")) {
lvl1.setChecked(true);
}
if(lvl.equals("2")) {
lvl2.setChecked(true);
}
if(lvl.equals("3")) {
lvl3.setChecked(true);
}
if(lvl.equals("4")) {
lvl4.setChecked(true);
}
if(lvl.equals("5")) {
lvl5.setChecked(true);
}
}
protected void playOnClick(View v) {
setContentView(R.layout.play);
setQuestion();
}
private String getLevel() {
String FILENAME = "think_level";
FileInputStream fis;
byte[] buffer = new byte[1000];
try {
fis = openFileInput(FILENAME);
} catch (FileNotFoundException e) {
setLevel("1");
return "-1";
}
try {
fis.read(buffer,0,1000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String level = buffer.toString();
return level;
}
private void setLevel(String _level) {
String FILENAME = "think_level";
String level = _level;
FileOutputStream fos;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(level.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void setQuestion() {
}
}
Here are my xml files:
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/imageView1" android:src="#drawable/think" android:scaleType="fitCenter"></ImageView>
<Button android:id="#+id/play" android:text="Play" android:layout_height="wrap_content" android:layout_width="wrap_content" ></Button>
<Button android:text="Level" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/level"></Button>
</TableLayout>
Here is my level.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="Level:" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</TableRow>
<TableRow android:id="#+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioGroup android:id="#+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioButton android:id="#+id/lvl1" android:text="Level 1" android:layout_height="wrap_content" android:checked="true" android:layout_width="wrap_content"></RadioButton>
<RadioButton android:id="#+id/lvl2" android:text="Level 2" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton>
<RadioButton android:id="#+id/lvl3" android:text="Level 3" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton>
<RadioButton android:id="#+id/lvl4" android:text="Level 4" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton>
<RadioButton android:id="#+id/lvl5" android:text="Level 5" android:layout_height="wrap_content" android:layout_width="wrap_content"></RadioButton>
</RadioGroup>
</TableRow>
<Button android:text="Set Level" android:id="#+id/updateLevel" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableLayout>
Here is play.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="#+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="TextView" android:id="#+id/question" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</ScrollView>
<RadioGroup android:id="#+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" >
<RadioButton android:id="#+id/radioButton1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton>
<RadioButton android:id="#+id/radioButton2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton>
<RadioButton android:id="#+id/radioButton3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton"></RadioButton>
</RadioGroup>
<Button android:id="#+id/go" android:text="Go" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
<Button android:id="#+id/tomenu" android:text="Back To Menu" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
</TableLayout>
The problem is that the view you are adding does not have a parent and therefore is not able to receive click events. Currently you're inflating the view using
View playv = inflater.inflate(R.layout.level, null);
You'll want to place a parent view in that second argument.
View playv = inflater.inflate(R.layout.level, parentView, false);
This will allow you to use a parent view but not actually attach it.
So the buttons are in your level and play layouts, and you're inflating those layouts, but it doesn't look like you're ever adding the layouts to the your main view/layout. Are you actually able to see those two layouts you are inflating?
inflation is async method, can you try to add the click listener after its inflated
//only inflate in oncreate / onresume
inflater.inflate(
R.layout.play, // resource
this, // root
true); //attach to root
//overide onFinishInflate and get the view here
protected void onFinishInflate() {
super.onFinishInflate();
Button gotomenu = (Button) levelv.findViewById(R.id.tomenu);
gotomenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toMenuOnClick(v);
}
});
}
There is no apparent reason why this should not work.
Any runtime created views must be added to your linearlayout(or any other parent layout)
using :
linearLayoutMain.addView(updateLevel);
Have you used this buttons in any of the linear layouts in your xml file i.e. does these buttons appear when you reach the corresponding activity?
You should be seeing the exception at tbl.addView(updateLevel);, because you are trying to add a view whose parent is already specified.
Are you not seeing this exception ?
If You want item value on item click just use .setTag(some value); and getTag();
RelativeLayout firstContactRlayout = (RelativeLayout) myLayout.findViewById(R.id.firstContactRlayout);
firstContactRlayout.setTag(number);
firstContactRlayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String gettags = (String) v.getTag();
Log.e("gettags", "--" + gettags);
}
});
Try this after tbl.addView(updateLevel); 0 of tbl.getChildAt(0) may change:
tbl.getChildAt(0).findViewById(R.id.updateLevel).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
updateLevelOnClick(v);
}
});