how to make static expandabletextview to dynamic expandaletextview - android

I create a static expandaletextview in this example i used two Textview and two ImageButton so now how to i make this dynamic expandable..Any one can help me please because i m very new in android... Here below is my full static source code thanks in advance
MainActivity class
package com.example.atextalignment;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView descText;
ImageButton show, hide;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
descText = (TextView) findViewById(R.id.description_text);
show = (ImageButton) findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Show button");
show.setVisibility(View.INVISIBLE);
hide.setVisibility(View.VISIBLE);
descText.setMaxLines(Integer.MAX_VALUE);
}
});
hide = (ImageButton) findViewById(R.id.hide);
hide.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Hide button");
hide.setVisibility(View.INVISIBLE);
show.setVisibility(View.VISIBLE);
descText.setMaxLines(5);
}
});
}
}
my Xml layout
<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="com.example.atextalignment.Second" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id="#+id/description_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:maxLines="5"
android:text="#string/desc_content" />
<ImageButton
android:id="#+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/description_text"
android:background="#android:color/transparent"
android:clickable="true"
android:src="#drawable/ic_launcher"/>
<ImageButton
android:id="#+id/hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/description_text"
android:clickable="true"
android:src="#drawable/ic_launcher"
android:visibility="invisible" />
</RelativeLayout>
String.xml
<string name="desc_content"> Android powers hundreds of millions of mobile devices in more than 190 countries around the world. It\'s the largest installed base of any mobile platform and growing fast—every day another million users power up their Android devices for the first time and start looking for apps, games, and other digital content.
Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.
</string>

Related

Fragments and Layouts

When trying to run our app, we consistently have errors with conflicting fragments not letting us run the activity. Here's a screencast of the problem I am facing
As you can see, the app itself contains a multitude of problems, but my primary focus is getting the clicker to respond on the click. Here is the class page for the clicker activity:
package com.example.android.Chapsnat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ContactsFragment extends AppCompatActivity {
Button btnClick;
Button btnReset;
TextView txtCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_contacts);
btnClick = (Button)findViewById(R.id.buttonClick);
btnReset = (Button)findViewById(R.id.buttonReset);
txtCount = (TextView)findViewById(R.id.textViewCount);
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String countValue = txtCount.getText().toString();
int intCountValue = Integer.parseInt(countValue);
intCountValue++;
txtCount.setText(String.valueOf(intCountValue));
} });
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtCount.setText(String.valueOf(0));
}
});
}
}
And this is the xml page it is being built in, which is also a fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_contacts"
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.android.Chapsnat.ContactsFragment">
<TextView
android:id="#+id/textViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="0"
android:textColor="#android:color/holo_purple"
android:textSize="40sp" />
<Button
android:text="Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonClick"
android:layout_below="#+id/textViewCount"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<Button
android:text="Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="171dp"
android:id="#+id/buttonReset"
android:layout_below="#+id/textViewCount"
android:layout_alignStart="#+id/buttonClick" />
</RelativeLayout>
Is there any way you can help? If so please let me know.

Suddently, "R.java" was deleted from my "CheckBox" program?

I'm learning Android (I'm a beginner) and making a program for CheckBox.
Everything seems fine, but when I cleaned my project suddenly, R.java was deleted and it's giving an error.
I checked three times, but not getting it back.
Here is my source code:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IPhone" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:checked="true"/>
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Windows Mobile"
android:checked="false"/>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display" />
</LinearLayout>
MainActivity.java
package com.example.lesson06;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnChkbox();
}
public void addListenerOnChkbox(){
CheckBox chkbx=(CheckBox)findViewById(R.id.checkBox1);
chkbx.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(((CheckBox) v).isChecked()){
Toast.makeText(MainActivity.this, "Bro..try Android :)", Toast.LENGTH_LONG);
}
}
});
}
public void addListenerOnButton(){
final CheckBox chkbx=(CheckBox)findViewById(R.id.checkBox1);
final CheckBox chkbx2=(CheckBox)findViewById(R.id.checkBox2);
final CheckBox chkbx3=(CheckBox)findViewById(R.id.checkBox3);
final Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
StringBuffer result=new StringBuffer();
result.append("IPhone check: ").append(chkbx.isChecked());
result.append("\nAndroid check: ").append(chkbx2.isChecked());
result.append("\nWindows Mobile Check: ").append(chkbx3.isChecked());
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
Any help would be greatly appreciated.
Your Button Tag not closed into activity_main.xml
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display"
Close that
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display" />
and clean project. Your R.java class will be generated.
Change your button to the following:
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display"/>
It seems you forgot to close the button tag, so AAPT could not generate a new R.class.
Generally R.java is not created when you have errors on one or more xml files. In this case the error is in the Button tag (the IDE should warn you about that error).

Android limit edittext length

I am having problem with limiting edittext length in android. Everything works perfect except when delete message.
My edit text control has max length of 16 characters and it will limit it for recognizing, displaying and reading first 16 characters but when I continue to type, it memorizes text somewhere in background and if I want to delete text it doesn't start to delete backwards from 16th character but from last one I have entered.
See below code.
I have even tried to add TextChangedListener it doesn't trigger 16 character is entered as there isn't any action.
I am using Nexus 7 for testing this functionality
LAYOUT
<?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:layout_gravity="center"
android:background="#drawable/background"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/send_new_message_lblTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/send_new_message_to"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/send_new_message_lblToValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/send_new_message_lblTo"
android:ems="10"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<EditText
android:id="#+id/send_new_message_txtMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/send_new_message_lblToValue"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="#string/send_new_message_hint"
android:inputType="textMultiLine"
android:lines="2"
android:maxLength="16"
android:scrollbarStyle="outsideOverlay" />
<Button
android:id="#+id/send_new_message_btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/send_new_message_txtMessage"
android:layout_marginTop="10dp"
android:text="#string/send" />
</RelativeLayout>
ACTIVITY
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class NewMessageSendActivity{
private TextView to;
private EditText message;
private Button send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message);
getViewInstances();
setControlActions();
}
private void getViewInstances() {
to = (TextView) findViewById(R.id.send_new_message_lblToValue);
message = (EditText) findViewById(R.id.send_new_message_txtMessage);
send = (Button) findViewById(R.id.send_new_message_btnSend);
to.setText(AppSettings.getInstance().getSelectedDevice().toString());
}
private void setControlActions() {
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String smsMessage = message.getText().toString();
Toast.makeText(this, smsMessage, Toast.LENGTH_SHORT).show();
}
});
}
}
I cannot reproduce your behavior. It seems everything is ok, and the text is correctly limited to 16 characters.
The only place where the keyboard keeps appending text, is in the field above it, but even when you select the suggested word, it is truncated before being inserted in the text field. That's why you don't see the event fired, because it's an internal event in the IME, not an Android one inside the EditText.

TextView moves one line down when text changes programmatically

This is a simple shell app that will display number of files in data/app folder when button is clicked the textviews are aligned as they should be, but when the button is clicked the textview that displays the number of files is moved down one line. Why?
Here is the code and the main.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="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="32dp"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:text="#string/user_apps" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView2"
android:text="0" />
</RelativeLayout>
code:
package rs.test.rootapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class RootAppActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String command[]={"su", "-c","ls /data/app | wc -l"};
Shell shell = new Shell();
String text = shell.sendShellCommand(command);
setNewTextInTextView(text);
}
});
}
public void setNewTextInTextView(String text){
//TextView tv= new TextView (this);
//tv.setText(text);
//setContentView(tv);
TextView tv =(TextView) findViewById(R.id.tv);
tv.setText(text);
}
}
Screenshots:
set the text in textview after done the trimming operation on it,may be this will work, try it
tv.setText(text.trim());
With looking very quickly it seems like the return from the command is coming with the text.. Try and replace the new line in text before setting the text in the TextView.

creating a simple layout on android

I'm very new with android development. I'm trying to develop an application, I decided to try out the code below but I keep getting errors when I try to open it on the emulator. please can anyone tell me what I'm doing wrong?
package hajara.android.MyRecipes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyRecipesActivity extends Activity {
Button btn;
TextView t1, t2;
EditText e;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
t1 = (TextView)findViewById(R.id.text1);
t2 = (TextView)findViewById(R.id.text2);
e = (EditText)findViewById(R.id.edit1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener((OnClickListener) this);
}
}
My main.xml file is
<?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:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter a string:"
/>
<EditText android:id="#+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
/>
<Button android:id="#+id/button1"
android:text="OK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
Your onClick listener is done incorrectly. I find it easier to simply create a method such as:
public void buttonOnClick(View v) {
// Do something
}
and within your XML layout file (ie. main.xml) invoke the onClick attribute:
<Button
...
android:onClick="buttonOnClick"
...
/>
I think you haven't Declared button Properly.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your Code Goes here
}
});
And Hope you have Declared your Activity in Manifest File.

Categories

Resources