[HELP] Program a Scrollable tabs - Android Q&A, Help & Troubleshooting

Hello guys,
I need your help to find how to code a Scrollable tabs (ics), I find no help and I'm looking but I can not find!
Had you any idea? Thank you in advance
http://developer.android.com/design/media/tabs_scrolly.mp4
Source: http://developer.android.com/design/patterns/actionbar.html#contextual

youpi01 said:
Hello guys,
I need your help to find how to code a Scrollable tabs (ics), I find no help and I'm looking but I can not find!
Had you any idea? Thank you in advance
http://developer.android.com/design/media/tabs_scrolly.mp4
Source: http://developer.android.com/design/patterns/actionbar.html#contextual
Click to expand...
Click to collapse
Create an actionbar and edit the layout to fit your needs.
Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle startInfo) {
super.onCreate(startInfo);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab tabCPU = actionBar.newTab()
.setText(R.string.cpu_tab_title)
.setTabListener(new TabListener<CpuInfoActivity>(
this, "artist", CpuInfoActivity.class));
actionBar.addTab(tabCPU);
Tab tabMEM = actionBar.newTab()
.setText(R.string.mem_tab_title)
.setTabListener(new TabListener<MemInfoActivity>(
this, "artist", MemInfoActivity.class));
actionBar.addTab(tabMEM);
}

thank you very much I will test it now!

Well I just tried but I can not, it shows me many errors!
Is there source code available for me to view?

Hello, I have made ​​some progress ...
I would just like to add tabs and be able to edit in xml!
How do I add Java code to the link to my different layout (tabs)
Attached is the file in question!
Code:
package com.youpi.app;
import com.actionbarsherlock.app.SherlockActivity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class AppActivity extends SherlockActivity implements OnPageChangeListener {
private ViewPager viewPager;
private static int NUM_VIEWS = 2;
private MyPagerAdapter pagerAdapter;
private TextView pageIndicator;
private int currentPage = 0;
private View[] tabViews = new View[NUM_VIEWS];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pagerAdapter = new MyPagerAdapter();
viewPager = (ViewPager) findViewById(R.id.viewpager1);
TextView textView1 = new TextView(this);
textView1.setText("Tunandroid View Pager Example");
textView1.setTextSize(20);
ImageView imageView1 = new ImageView(this);
imageView1.setImageResource(R.drawable.ic_launcher);
[COLOR="Red"]tabViews[0] = textView1;
tabViews[1] = imageView1;
tabViews[2]= here add a new tab but in xml![/COLOR]
viewPager.setAdapter(pagerAdapter);
viewPager.setOnPageChangeListener(this);
viewPager.setCurrentItem(currentPage);
pageIndicator = (TextView)findViewById(R.id.pageIndicator);
pageIndicator.setText(String.valueOf(currentPage+1));
}
private class MyPagerAdapter extends PagerAdapter
{
@Override
public int getCount() {
return NUM_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
((ViewPager) collection).addView(tabViews[position],0);
return tabViews[position];
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View)view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==(object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
currentPage = position;
pageIndicator.setText(String.valueOf(currentPage+1));
}
}

Related

[Q] BroadcastReceiver question

The first version of a small program. MyBroadcastReceiver is invoked by sendBroadcast(new Intent(MyBroadcastReceiver.ACTION)). It works.
Code:
package com.example.broadcast;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MyActivity extends Activity
{
private final BroadcastReceiver mBroadcastReceiver = new MyBroadcastReceiver();
private class MyBroadcastReceiver extends BroadcastReceiver
{
public static final String ACTION = "com.example.broadcast.MyBroadcastReceiver.ACTION";
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "onReceive() fired", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(mBroadcastReceiver, new IntentFilter(MyBroadcastReceiver.ACTION));
((Button)findViewById(R.id.testButton)).setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
sendBroadcast(new Intent(MyBroadcastReceiver.ACTION));
}
});
}
@Override
protected void onDestroy()
{
unregisterReceiver(mBroadcastReceiver);
super.onDestroy();
}
}
The second version of the same program. Now MyBroadcastReceiver is invoked by sendBroadcast(new Intent(MyActivity.this, MyBroadcastReceiver.class)) and IntentFilter in call to registerReceiver() is empty. It doesn't work. Why? And what do I have to change to make it work?
Code:
public class MyActivity extends Activity
{
private final BroadcastReceiver mBroadcastReceiver = new MyBroadcastReceiver();
private class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "onReceive() fired", Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(mBroadcastReceiver, new IntentFilter());
((Button)findViewById(R.id.testButton)).setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
sendBroadcast(new Intent(MyActivity.this, MyBroadcastReceiver.class));
}
});
}
@Override
protected void onDestroy()
{
unregisterReceiver(mBroadcastReceiver);
super.onDestroy();
}
}

[Q] somebody help me im losing steam!!

problem is in CFGameView???
hi, i will make this short but to the point...
making a 2D app with a space theme and involves making a Game View which needs to use two images to make the background which is scrolling in portrait.
im stuck on the CFGameView part at the bottom.
i think i need to link them some how??
its blatantly simple and im just being thick..
cheers
-----------------CFGameRenderer-----------
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class CFGameRenderer implements Renderer {
private CFBackground background = new CFBackground();
private CFBackground background2 = new CFBackground();
private float bgScroll1;
private float bgScroll2;
@Override
public void onDrawFrame(GL10 gl) { try { Thread.sleep(CFEngine.GAME_THREAD_FPS_SLEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
scrollBackground1(gl);
scrollBackground2(gl);
//ALL OTHER GAME DRAWING WILL BE CALLED HERE
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
}
private void scrollBackground1(GL10 gl){
if (bgScroll1 == Float.MAX_VALUE){
bgScroll1 = 0f;
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(1f, 1f, 1f);
gl.glTranslatef(0f, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, bgScroll1, 0.0f);
background.draw(gl);
gl.glPopMatrix();
bgScroll1 += CFEngine.SCROLL_BACKGROUND1;
gl.glLoadIdentity();
}
private void scrollBackground2(GL10 gl){
if (bgScroll2 == Float.MAX_VALUE){
bgScroll2 = 0f;
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(.5f, 1f, 1f);
gl.glTranslatef(1.5f, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, bgScroll2, 0.0f);
background2.draw(gl);
gl.glPopMatrix();
bgScroll2 += CFEngine.SCROLL_BACKGROUND2;
gl.glLoadIdentity();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0f, 1f, 0f, 1f, -1f, 1f);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
background.loadTexture(gl, CFEngine.BACKGROUND_LAYER_ONE, CFEngine.context);
background2.loadTexture(gl, CFEngine.BACKGROUND_LAYER_TWO, CFEngine.context);
}
}
---------------- CFGame-------------
import android.app.Activity;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
public class CFGame extends Activity {
private CFGameView gameView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gameView = new CFGameView(this);
setContentView(gameView);
}
@Override
protected void onResume() {
super.onResume();
gameView.onResume();
}
@Override
protected void onPause() {
super.onPause();
gameView.onPause();
}
}
--------------- CFGamView ---------------
import android.content.Context;
import android.opengl.GLSurfaceView;
public class CFGameView extends GLSurfaceView{
public CFGameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
basically how do i properly create and extend the GLSurfaceView so it starts my game.
i keep getting this logcat error no matter what i try.
03-09 17:42:16.649: E/AndroidRuntime(17570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blah blah/com.blah blah.CFGame}: java.lang.NullPointerException

android quiz help

So I've decided to to teach myself how to program for android and have a general understanding for java. Well i decided to borrow someones code from the internet and utilize it to assist in creating my first quiz app. I am getting some errors in this code and was wondering if anyone would be so kind to assist me. Thanks
Code:
package com.danyluk.MedicStudyGuide;
import java.io.IOException;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Quiz extends Activity{
/** Called when the activity is first created. */
private RadioButton radioButton;
private TextView quizQuestion;
private TextView tvScore;
private int rowIndex = 1;
private static int score=0;
private int questNo=0;
private boolean checked=false;
private boolean flag=true;
private RadioGroup radioGroup;
String[] corrAns = new String[5];
final DataBaseHelper db = new DataBaseHelper(this);
Cursor c1;
Cursor c2;
Cursor c3;
int counter=1;
String label;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
String options[] = new String[19];
// get reference to radio group in layout
RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c3 = db.getCorrAns();
tvScore = (TextView) findViewById(R.id.tvScore);
for (int i=0;i<=4;i++)
{
corrAns[i]=c3.getString(0);
c3.moveToNext();
}
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0;;){
RadioButton btn = (RadioButton) radioGroup.getChildAt(i);
String text;
if (btn.isPressed() && btn.isChecked() && questNo < 5)
{
Log.e("corrAns[questNo]",corrAns[questNo]);
if (corrAns[questNo].equals(btn.getText()) && flag==true)
{
score++;
flag=false;
checked = true;
}
else if(checked==true)
{
score--;
flag=true;
checked = false;
}
}
}
tvScore.setText = ("Score: " + Integer.toString(score) + "/5");
Log.e("Score:", Integer.toString(score));
}
});
quizQuestion = (TextView) findViewById(R.id.TextView01);
displayQuestion();
/*Displays the next options and sets listener on next button*/
Button btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(btnNext_Listener);
/*Saves the selected values in the database on the save button*/
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(btnSave_Listener);
}
/*Called when next button is clicked*/
private View.OnClickListener btnNext_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {
flag=true;
checked = false;
questNo++;
if (questNo < 5)
{
c1.moveToNext();
displayQuestion();
}
}
};
/*Called when save button is clicked*/
private View.OnClickListener btnSave_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
private void displayQuestion()
{
//Fetching data quiz data and incrementing on each click
c1=db.getQuiz_Content(rowIndex);
c2 =db.getAns(rowIndex++);
quizQuestion.setText(c1.getString(0));
radioGroup.removeAllViews();
for (int i=0;i<=3;i++)
{
//Generating and adding 4 radio buttons dynamically
radioButton = new RadioButton(this);
radioButton.setText(c2.getString(0));
radioButton.setId(i);
c2.moveToNext();
radioGroup.addView(radioButton);
}
}}}
You forgot give us the error that you get

[Q] Camera stream

Hi,
I have to develop an application for my studies which consists in capture and broadcasting live on Web via an android phone
At present my code allows me to capture but I do not manage to broadcast the video on a server
Can you help me ?
Code:
package com.androidvideocapture;
import java.io.IOException;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class AndroidVideoCapture extends Activity implements SurfaceHolder.Callback{
Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean recording;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recording = false;
mediaRecorder = new MediaRecorder();
initMediaRecorder();
setContentView(R.layout.activity_android_video_capture);
SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
surfaceHolder = myVideoView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
myButton = (Button)findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
}
private Button.OnClickListener myButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(recording){
mediaRecorder.stop();
mediaRecorder.release();
finish();
}else{
mediaRecorder.start();
recording = true;
myButton.setText("STOP");
}
}};
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
prepareMediaRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
private void initMediaRecorder(){
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mediaRecorder.setProfile(camcorderProfile_HQ);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
}
private void prepareMediaRecorder(){
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Android Studio Noob Question: Btn 2 Activity

Hey Im very new to Android studio, and Im trying to understand the procces of building a app
So here is what I learned so far.
1. I start with the MainActivty
2. Add Activity Cycle: onCreate, onStart, onResume etc.
3. Import android.util.Log; so I can see the log information for my code
4. Create a Second Activity
5. Create a btn on the MainActivity and onClick move to Second Activity.
Check the code below to say how I set up my MainActivity
Now here is the problem, when i run the Emulator:
- there are no errors
Press button to go to the other Activity, my log shows:
MainActivity_message: onClick
MainActivity_message: onPause
SecondActivity_message: onCreate
SecondActivity_message: onStart
SecondActivity_message: onResume
BUT, in my Emulator viewer the MainActivity stays visible and Im not seeing the SecondAcivity.
How do i solve this or what im I missing here?
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button BtnMove; //
public static final String TAG = "MainActivity_message";
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate"); //
BtnMove = findViewById(R.id.BTNActivityOne); //
BtnMove.setOnClickListener(new View.OnClickListener() { //
@override
public void onClick(View v) {
moveToActivityTwo(); //
Log.i(TAG, "onClick"); //
}
});
}
private void moveToActivityTwo() { //
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
@override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart"); //
}
@override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume"); //
}
@override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause"); //
}
@override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop"); //
}
@override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart"); //
}
@override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy"); //
}
}

Categories

Resources